package com.shop.shop_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.shop_end.entity.Cate;
import com.shop.shop_end.exception.ServiceException;
import com.shop.shop_end.mapper.CateMapper;
import com.shop.shop_end.service.CateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shop_end.utils.GenerateIdUtil;
import com.shop.shop_end.utils.result.Result;
import com.shop.shop_end.utils.result.ResultUtil;
import com.shop.shop_end.variable.EnableVariable;
import com.shop.shop_end.variable.ErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2024-04-23
 */
@Service
public class CateServiceImpl extends ServiceImpl<CateMapper, Cate> implements CateService {
    final Logger logger = LoggerFactory.getLogger(CateServiceImpl.class);

    @Resource
    private CateMapper cateMapper;

    /**
     * 获取所有cate
     * @return
     */
    @Override
    public List<Cate> getAllCate() {
        List<Cate> cateList = this.cateMapper.selectList(new QueryWrapper<>());
        logger.info("cateList==>{}", cateList);
        return cateList;
    }

    /**
     * 增加一个分类
     * @param cate
     * @return
     */
    @Override
    @Transactional
    public Result addOneCate(Cate cate){
        logger.info("开始增加一个分类");
        cate.setCateId(GenerateIdUtil.generateId());
        cate.setCreateTime(new Date());
        logger.info("cate==>{}", cate);
        try {
            int insert = this.cateMapper.insert(cate);
            if (insert == EnableVariable.UNABLE){
                logger.info("增加cate失败");
                return ResultUtil.fail("增加cate失败");
            }
        }catch (Exception e){
            logger.info("增加cate出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_INSERT_ERROR, "插入cate出现异常");
        }
        logger.info("增加成功");
        return ResultUtil.ok("增加成功");
    }

    /**
     * 根据id删除cate
     * @param cateId
     * @return
     */
    @Override
    @Transactional
    public Result deleteCateById(Long cateId){
        logger.info("开始根据id删除cate，cateId==>{}", cateId);
        try {
            int delete = this.cateMapper.deleteById(cateId);
            if (delete == EnableVariable.UNABLE){
                logger.info("删除cate失败");
                return ResultUtil.fail("删除cate失败");
            }
        }catch (Exception e){
            logger.info("删除cate出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_DELETE_ERROR, "删除cate出现异常");
        }
        logger.info("删除成功");
        return ResultUtil.ok("删除成功");
    }

    /**
     * 根据id修改cate
     * @param cate
     * @return
     */
    @Override
    @Transactional
    public Result updateCateById(Cate cate){
        logger.info("开始根据id修改cate，cate==>{}", cate);
        try {
            int update = this.cateMapper.updateById(cate);
            if (update == EnableVariable.UNABLE){
                logger.info("修改cate失败");
                return ResultUtil.fail("修改cate失败");
            }
        }catch (Exception e){
            logger.info("修改cate出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_UPDATE_ERROR, "修改cate出现异常");
        }
        logger.info("修改成功");
        return ResultUtil.ok("修改成功");
    }


    /**
     * 根据id查询出cate
     * @param cateId
     * @return
     */
    @Override // 继承接口的按主键查询 返回Entity实例
    public Cate getCateById(Long cateId) {
        logger.info("cateId==>{}", cateId);
        return this.cateMapper.selectById(cateId);
    }

}
